package ms.maplestory.boot.client.character;

import ms.maplestory.boot.client.Account;
import ms.maplestory.boot.client.Client;
import ms.maplestory.boot.client.User;
import ms.maplestory.boot.client.character.Inventory.Inventory;
import lombok.Getter;
import lombok.Setter;
import ms.maplestory.boot.client.character.skills.Skill;
import ms.maplestory.boot.packets.universal.CalcDamage;
import ms.maplestory.boot.service.CharService;
import ms.maplestory.boot.util.Position;
import ms.maplestory.boot.util.SpringUtil;
import ms.maplestory.boot.world.field.Field;

import javax.persistence.*;

import java.util.Set;

import static ms.maplestory.boot.enums.InventoryType.*;

@Getter
@Setter
@Entity
@Table(name = "characters")
public class Char {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "userId")
    private User user;

    @JoinColumn(name = "stat")
    @OneToOne(cascade = CascadeType.REMOVE)
    private CharStat stat;

    @JoinColumn(name = "equippedInventory")
    @OneToOne(cascade = CascadeType.REMOVE)
    private Inventory equippedInventory = new Inventory(Equipped, 52);

    @JoinColumn(name = "equipInventory")
    @OneToOne(cascade = CascadeType.REMOVE)
    private Inventory equipInventory = new Inventory(Equip, 52);

    @JoinColumn(name = "useInventory")
    @OneToOne(cascade = CascadeType.REMOVE)
    private Inventory useInventory = new Inventory(Use, 52);

    @JoinColumn(name = "etcInventory")
    @OneToOne(cascade = CascadeType.REMOVE)
    private Inventory etcInventory = new Inventory(Etc, 52);

    @JoinColumn(name = "setupInventory")
    @OneToOne(cascade = CascadeType.REMOVE)
    private Inventory setupInventory = new Inventory(Setup, 52);

    @JoinColumn(name = "cashInventory")
    @OneToOne(cascade = CascadeType.REMOVE)
    private Inventory cashInventory = new Inventory(Cash, 96);

    @OneToMany(cascade = CascadeType.REMOVE, mappedBy = "chr")
    private Set<Skill> skills;

    @Transient
    private CharService charService;
    @Transient
    private Client client;
    @Transient
    private Account account;
    @Transient
    private boolean changingChannel;
    @Transient
    private Ranking ranking;
    @Transient
    private Position position;
    @Transient
    private Field field;
    @Transient
    private CalcDamage calcDamage;

    public CharService getCharService() {
        if (charService == null) {
            charService = SpringUtil.getBean("charService", CharService.class);
        }
        return charService;
    }

    @Override
    public String toString() {
        return getClass().getName() + "{" +
                "(" + super.toString() +
                ")id=" + id +
                ", userId=" + user.getId() +
                ", name=" + getStat().getName() +
                '}';
    }
}
